package com.game.logic.items.pet;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.constant.CurrencyType;
import com.coment.cfg.luban.items.PetCfg;
import com.coment.game.fight.AttributeManagerLogic;
import com.coment.game.fight.Attrs;
import com.coment.po.UserPet;
import com.game.dao.user.UserPlayInfoDao;
import com.game.dao.user.pet.UserPetDao;
import com.game.framework.ActorManager;
import com.game.logic.fighting.attrs.AttrsManagement;
import com.game.logic.items.account.UserAccountActor;
import com.coment.message.account.PayInfo;
import com.xs.action.exection.ActionException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/// 3.宠物服务
@Slf4j
@Service
public class PetService{
    @Resource
    private UserPetDao userPetDao;

    @Resource
    private UserPlayInfoDao userPlayInfoDao;

    @Resource
    private AttrsManagement attrsManagement;

    /// 获取宠物
    public UserPet tryObtainPet(Integer userInfoId, Integer petId) {
        Optional<PetCfg> petCfg = Optional.ofNullable(Tables.cfg.get_tbpetcfg().get(petId));
        if (petCfg.isEmpty()) {
            throw new ActionException("没有此宠物"+petId);
        }

        Optional<UserPet> userPet = userPetDao.findUserPetByUserInfoIdAndPetId(userInfoId, petId);
        if (userPet.isEmpty()){
            UserPet pet = new UserPet(userInfoId, petId, 1);
            return userPetDao.insert(pet);
        }else {
            petLevelUp(userInfoId, petId);
        }

        return userPet.get();
    }

    /// 宠物升级
    public UserPet petLevelUp(
            Integer userInfoId,
            int petId
    ) {
        Optional<UserPet> userPet = userPetDao.findUserPetByUserInfoIdAndPetId(userInfoId, petId);
        if (userPet.isEmpty()){
            throw new ActionException("没有此宠物");
        }
        UserPet pet = userPet.get();
        pet.lv++;


        Optional<PetCfg> petCfg = Optional.ofNullable(Tables.cfg.get_tbpetcfg().get(pet.getPetId()));
        if (petCfg.isEmpty()) {
            throw new ActionException("没有此等级的宠物");
        }

        PayInfo pay = ActorManager.ask(UserAccountActor.class, new PayInfo(userInfoId, CurrencyType.lg, 1, "宠物升级"), userInfoId);

        if (!pay.isPay()) {
            throw new ActionException(pay.getMessage());
        }

        userPetDao.save(pet);


        Attrs cfgAttrs = AttributeManagerLogic.obtainCfgAttrs(petCfg.get());
        Attrs userPetAttrs = new Attrs();
        // * 等级
        cfgAttrs.forEach((key, value) -> userPetAttrs.put(key, value * pet.getLv()));

        // 记录全局属性
        attrsManagement.logAttrsGlobal(
                userInfoId,
                userPetAttrs,
                "pet:", petId
        );

        return pet;
    }

    public UserPet fight(Integer userInfoId, int petId) {
        if (!List.of(19, 20, 21, 22).contains(petId)) {
            throw new ActionException("不能出战");
        }

        UserPet userPet = userPetDao.findUserPetByUserInfoIdAndPetId(userInfoId, petId).orElseThrow(() -> new ActionException("没有此宠物"));
        userPlayInfoDao.updateRecentlyPlayedPetId(userPet.getUserInfoId(), petId);
        return userPet;
    }
}
